<?php
namespace LPCISII\PHP2012Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use LPCISII\PHP2012Bundle\Entity\Recherche;
use LPCISII\PHP2012Bundle\Entity\Articles;
use LPCISII\PHP2012Bundle\Form\RechercheType;
use LPCISII\PHP2012Bundle\Form\ArticlesType;

class ArticlesController extends Controller {
    
    private $modes = array(
        'short' => 'short',
        'detailed' => 'detailed'
    );
    
    public function indexAction($idCa) {
        $em = $this->getDoctrine()->getEntityManager();
        $rpa = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
        $rpc = $em->getRepository('LPCISIIPHP2012Bundle:Categories');
        
        $articles = $rpa->findByCategorie($idCa);
/*<<<<<<< HEAD
         
        return $this->render('LPCISIIPHP2012Bundle:Articles:index.html.twig', array(
            'articles' => $articles
=======*/
        $categorie = $rpc->findOneBy(array(
           'idCa' => $idCa 
//>>>>>>> ef868e65951cf8e7c2218597f5cb549b05045382
        ));
        
        if(is_object($categorie)) {
            $mode = (isset($_GET['mode'])) ? $_GET['mode'] : 'short';
            if(!isset($this->modes[$mode])) $mode = 'short';
            return $this->render('LPCISIIPHP2012Bundle:Articles:index.html.twig', array(
                'articles' => $articles,
                'selected' => $categorie,
                'mode' => $mode
            ));
        } else {
            return $this->render('LPCISIIPHP2012Bundle:Default:index.html.twig');
        }
    }
    
    public function articleAction($reference, $mode) {
        $em = $this->getDoctrine()->getEntityManager();
        $rpa = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
        $article = $rpa->findOneBy(array(
           'reference' => $reference 
        ));
        
        if(is_object($article)) {
            if(!isset($this->modes[$mode])) $mode = 'short';
            return $this->render('LPCISIIPHP2012Bundle:Articles:article.html.twig', array(
                'article' => $article,
                'mode' => $mode,
            ));
        } else {
            return $this->render('LPCISIIPHP2012Bundle:Default:index.html.twig');
        }
    }
    
    public function rechercheAction() {
        
        $recherche = new Recherche();
        $form = $this->createForm(new RechercheType(), $recherche);
        
        $request = $this->getRequest();
        
        if ($request->getMethod() == 'POST') {
            
            $form->bindRequest($request);

            if ($form->isValid()) {
                
                $em = $this->getDoctrine()->getEntityManager();
                $rpa = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
                $nom = $form->getData()->getNomArticle();
                
                $articles = $rpa->findByNom($nom);
                
                return $this->render('LPCISIIPHP2012Bundle:Articles:index.html.twig', array(
                   'form' => $form->createView(),
                   'articles' => $articles,
                   'message' => 'Résultats de la recherche sur "'.$nom.'"',
                   'mode' => 'short',
                   'selected' => null
                ));
            }
        }
        return $this->render('LPCISIIPHP2012Bundle:Articles:recherche.html.twig', array(
            'form' => $form->createView()
        ));
    }
    
    public function addAction($reference){

      if(!$this->getRequest()->cookies->has("panier")){
	$panier = serialize(array());
	setcookie("panier", $panier, time()+3600*24*365,"/");
      }
      $em = $this->getDoctrine()->getEntityManager();
      $ar = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
      $article = $ar->findByReference($reference);
      $panier = $this->getRequest()->cookies->get("panier");
      $panier = unserialize($panier);
      
      $exist = 0;

      foreach ($panier as $key => $art){
	
	if($art['id'] == $reference){
	    $exist = 1;
	    $panier[$key]['qte']++;
	 }
      }
      if(!$panier) $panier = array();
	if(!$exist) {
	  $nar = array();
	  $nar['id'] = $reference;
	  $nar['qte'] = 1;
	  $nar['nom'] = $article[0]->getNom();
	  $nar['prix'] = $article[0]->getPrix();
	  $panier[] = $nar;
	}
	$cookies = $this->getRequest()->cookies;
	$seriapanier = serialize($panier);
	setcookie("panier", $seriapanier, time()+3600*24*365,"/");
	return $this->render("LPCISIIPHP2012Bundle:Page:panier.html.twig", array('panier' => $panier));
    }

    public function delAction($reference){
      if(!$this->getRequest()->cookies->has("panier")){
	$panier = serialize(array());
	setcookie("panier", $panier, time()+3600*24*365,"/");
      }
      $panier = $this->getRequest()->cookies->get("panier");
      $panier = unserialize($panier);
     
      if($panier != null){
	foreach ($panier as $key => $art){
	
	  if($art['id'] == $reference){
              
	    unset($panier[$key]);
	    $seriapanier = serialize($panier);
	    setcookie("panier", $seriapanier, time()+3600*24*365,"/");
	}
	  
	  
      }
      }
      return $this->render("LPCISIIPHP2012Bundle:Page:panier.html.twig", array( 'panier' => $panier));
    }

     public function articleSeulAction($reference) {
        $mode = (isset($_GET['mode'])) ? $_GET['mode'] : 'short';
        if(!isset($this->modes[$mode])) $mode = 'short';
        
        return $this->render('LPCISIIPHP2012Bundle:Articles:articleseul.html.twig', array(
            'reference' => $reference,
            'mode' => $mode
        ));
    }
    
    public function afficherPanierAction() {
        if(!$this->getRequest()->cookies->has("panier")){
            $panier = serialize(array());
            setcookie("panier", $panier, time()+3600*24*365,"/");
        }
        $panier = $this->getRequest()->cookies->get("panier");
        $panier = unserialize($panier);
        if(!$panier) $panier = array();
        $cookies = $this->getRequest()->cookies;
	$seriapanier = serialize($panier);
	setcookie("panier", $seriapanier, time()+3600*24*365,"/");
	return $this->render("LPCISIIPHP2012Bundle:Page:panier.html.twig", array('panier' => $panier));
    
    }
}
//=======
//>>>>>>> ef868e65951cf8e7c2218597f5cb549b05045382
